// @ts-nocheck
import { useUserStore } from '@/stores'
import { createRouter, createWebHistory } from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

NProgress.configure({ showSpinner: false })

// history模式 createWebHistory
// hash模式 createWebHashHistory
/*          history   hash
 * 带#      不带       带
 * 后端支持  需要       不需要
 *
 */

// console.log(import.meta.env.BASE_URL)

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  /**
   * 配置一级路由
   * 1. 创建页面组件 playground页面
   * 2. 路径 组件 配置规则
   * 3. 路由出口
   */
  routes: [
    {
      path: '/login',
      component: () => import('@/views/Login/index.vue'),
      meta: { title: '优医问诊-登录页' }
    },
    {
      path: '/consult/fast',
      component: () => import('@/views/Consult/ConsultFast.vue'),
      meta: { title: '极速问诊' }
    },
    {
      path: '/consult/dep',
      component: () => import('@/views/Consult/ConsultDep.vue'),
      meta: { title: '选择科室' }
    },
    {
      path: '/consult/illness',
      component: () => import('@/views/Consult/ConsultIllness.vue'),
      meta: { title: '病情描述' }
    },
    {
      path: '/consult/pay',
      component: () => import('@/views/Consult/ConsultPay.vue'),
      meta: { title: '问诊支付' }
    },
    {
      path: '/room',
      component: () => import('@/views/Room/index.vue'),
      meta: { title: '问诊室' },
      beforeEnter(to) {
        // console.log(to)
        if (to.query.payResult === 'false') {
          return '/user/consult'
        }
      }
    },
    {
      path: '/login/callback',
      component: () => import('@/views/Login/LoginCallback.vue'),
      meta: { title: 'QQ登录-绑定手机' }
    },
    {
      path: '/',
      component: () => import('@/views/Layout/index.vue'),
      redirect: '/home',
      children: [
        {
          path: '/user',
          component: () => import('@/views/User/index.vue'),
          meta: { title: '优医问诊-用户中心' }
        },
        {
          path: '/home',
          component: () => import('@/views/Home/index.vue'),
          meta: { title: '优医问诊-首页' }
        },
        {
          path: '/article',
          component: () => import('@/views/Article/index.vue'),
          meta: { title: '优医问诊-健康百科' }
        },
        {
          path: '/notify',
          component: () => import('@/views/Notify/index.vue'),
          meta: { title: '优医问诊-消息通知' }
        }
      ]
    },
    {
      path: '/user/patient',
      component: () => import('@/views/User/PatientPage.vue'),
      meta: { title: '优医问诊-家庭档案' }
    },

    {
      path: '/playground',
      component: () => import('@/views/Playground/index.vue')
    }
  ]
})
const whileList = ['/login', '/login/callback']
router.beforeEach((to) => {
  NProgress.start()
  const useStore = useUserStore()
  if (!useStore.user?.token && !whileList.includes(to.path)) return '/login'
})
router.afterEach((to) => {
  // console.log(to.meta.title)
  document.title = to.meta.title || '优医问诊'
  NProgress.done()
})

export default router
